package com.kehongborui.mybatisspring.controller;

import com.kehongborui.mybatisspring.mapper.UserMapper;
import com.kehongborui.mybatisspring.model.UserModel;
import com.kehongborui.mybatisspring.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-08-03
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;
    /**
     * 获取登录页面
     * @return
     */
    // @RequestMapping(method = RequestMethod.GET)
    @GetMapping
    public String login() {
        return "login";
    }

    /**
     * 获取用户表单提交登录数据
     * @param model
     * @param request
     * @return
     */
    @PostMapping
    public String toLogin(UserModel model, HttpServletRequest request, ModelMap map) {
        UserModel user = loginService.login(model);
        if (user == null) {
            map.addAttribute("msg", "用户名不存在");
            map.addAttribute("username", model.getUsername());
            return "login";
        }

        return "redirect:login/success";
    }

    @GetMapping("/success")
    public String success() {
        return "success";
    }
}
